package com.bawei.module_home.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.bawei.module_home.databinding.ItemServiceBinding
import com.bawei.module_home.model.HomeServiceModel

class HomeServiceAdapter:RecyclerView.Adapter<HomeServiceViewHolder>() {
    private val list = mutableListOf<HomeServiceModel>()
    // 定义点击回调接口
    interface OnItemClickListener {
        fun onItemClick(item: HomeServiceModel, position: Int)
    }
    var itemClickListener: OnItemClickListener? = null
    // 设置点击监听器
    fun setOnItemClickListener(listener: OnItemClickListener) {
        this.itemClickListener = listener
    }
    fun setData(data:List<HomeServiceModel>){
        list.clear()
        list.addAll(data)
        notifyDataSetChanged()
    }

    override fun getItemCount(): Int {
        return list.size
    }



    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HomeServiceViewHolder {
        val binding = ItemServiceBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
        return HomeServiceViewHolder(binding)
    }
    override fun onBindViewHolder(holder: HomeServiceViewHolder, position: Int) {
        val binding=DataBindingUtil.getBinding<ItemServiceBinding>(holder.itemView) ?: return
        binding.serviceI1.setImageResource(list[position].img)
        binding.serviceT1.text=list[position].title
        // 设置点击事件
        holder.itemView.setOnClickListener {
            itemClickListener?.onItemClick(list[position], position)
        }
    }




}
class HomeServiceViewHolder(val binding: ItemServiceBinding) : RecyclerView.ViewHolder(binding.root)














